/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.session;

import com.velocityme.entity.Activity;
import com.velocityme.entity.ChangeDelta;
import com.velocityme.entity.Node;
import com.velocityme.entity.Person;
import com.velocityme.entity.Time;
import com.velocityme.enums.ChangeDeltaItemType;
import com.velocityme.enums.ChangeDeltaType;
import com.velocityme.enums.Permission;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author rcrida
 */
@Stateless
public class PersonBean implements PersonLocal {
    @EJB
    private SearchLocal searchBean;

    @EJB
    private ContactableLocal contactableBean;

    @PersistenceContext
    private EntityManager em;

    @Override
    public Collection<Person> findAllPersons(KeyLocal key) {
        key.validateKey();
        return em.createNamedQuery("findAllPersons").getResultList();
    }

    public Person findPerson(KeyLocal key, Long personId) {
        Person person = em.find(Person.class, personId);
        key.assertHasPermission(person, Permission.NODE_VIEW);
        return person;
    }

    public Person createPerson(KeyLocal key, Person person) {
        key.assertHasPermission(person.getParentNode(), Permission.PERSON_CREATE);
        person.setName(person.getFirstName() + " " + person.getLastName());
        ChangeDelta changeDelta = person.addChangeDelta(ChangeDeltaType.CREATE, key.getUser());
        contactableBean.logCreate(person, changeDelta);
        changeDelta.addItem(ChangeDeltaItemType.FIRST_NAME, person.getFirstName());
        changeDelta.addItem(ChangeDeltaItemType.LAST_NAME, person.getLastName());
        changeDelta.addItem(ChangeDeltaItemType.JOB_TITLE, person.getJobTitle());
        em.persist(person);
        em.persist(changeDelta);
        return person;
    }

    public void editPerson(KeyLocal key, Person person) {
        Person current = em.find(Person.class, person.getId());
        key.assertHasPermission(person, Permission.PERSON_EDIT);
        person.setName(person.getFirstName() + " " + person.getLastName());
        ChangeDelta changeDelta = person.addChangeDelta(ChangeDeltaType.EDIT, key.getUser());
        contactableBean.logEdit(person, current, changeDelta);
        if (!person.getFirstName().equals(current.getFirstName())) {
            changeDelta.addItem(ChangeDeltaItemType.FIRST_NAME, person.getFirstName());
        }
        if (!person.getLastName().equals(current.getLastName())) {
            changeDelta.addItem(ChangeDeltaItemType.LAST_NAME, person.getLastName());
        }
        if (!person.getJobTitle().equals(current.getJobTitle())) {
            changeDelta.addItem(ChangeDeltaItemType.JOB_TITLE, person.getJobTitle());
        }
        em.persist(changeDelta);
        em.merge(person);
    }

    public Collection<Time> getPersonTimes(KeyLocal key, Person person, Date start, Date stop) {
        key.validateKey();
        return em.createNamedQuery("findPersonTimeByDate").
                setParameter("person", person).
                setParameter("start", start).
                setParameter("stop", stop).
                getResultList();
    }

    public Collection<Activity> getPersonActivities(KeyLocal key) {
        key.validateKey();
        Set<Activity> activities = new TreeSet<Activity>(new Comparator<Activity>() {
            public int compare(Activity o1, Activity o2) {
                return (o1.getProjectPathName()+o1.getName()).compareTo(o2.getProjectPathName()+o2.getName());
            }
        });
        activities.addAll(searchBean.findAllMyResponsibleTasks(key));
        for (Node node: (List<Node>) em.createNamedQuery("findBookmarkNodesForUser").setParameter("user", key.getUser()).getResultList()) {
            if (node instanceof Activity)
                activities.add((Activity)node);
        }
        return activities;
    }


 
}
